<template>
  <div id="agreement" class='agreement'>
    <div class="title">{{ props.agreementTitle }}</div>
    <div class="content">
      <div v-for="(item, index) in props.contentList"
        :class="['items', props.indexList.includes(index + 1) ? 'mt-30' : '']" :key="item">
        {{ item }}
      </div>
    </div>
    <div class="party-a">
      <div class="ft">
        <span>甲方：方田教育</span>
      </div>
      <div class="date">
        <span>日期：</span>
        <span>{{ currentDay[0] }}</span>
        <span>年</span>
        <span>{{ currentDay[1] }}</span>
        <span>月</span>
        <span>{{ currentDay[2].split(" ")[0] }}</span>
        <span>日</span>
      </div>
      <img src="../assets/img/agreement/seal.png" alt="" srcset="" />
    </div>
    <div class="party-b">
      <div class="ft">
        <div class="sign">
          <span>乙方：</span>
          <!-- v-show="!signature" -->
          <span class="start-sign" v-show="!signature"></span>
          <img :src="signature" class="sign-img" v-if="signature" alt="img" crossOrigin="anonymous" />
        </div>
      </div>
      <div class="date">
        <span>日期：</span>
        <span>{{ currentDay[0] }}</span>
        <span>年</span>
        <span>{{ currentDay[1] }}</span>
        <span>月</span>
        <span>{{ currentDay[2].split(" ")[0] }}</span>
        <span>日</span>
      </div>
    </div>


    <van-back-top v-if="!showSignature" target="#agreement">
      <template #default>
        <img src="../assets/img/toTop.png" class="back-to-top-img" alt="" srcset="" />
      </template>
    </van-back-top>
    <div class="sub-btn" @click="!signature ? startSignature() : dosub()">
      <div v-if="!signature">
        <span class="iconfont icon-bi"> </span>
        点击开始手写签名
      </div>
      <div v-else>确定</div>
    </div>
    <div style="height: 20px;"></div>
    <div v-if="showSignature" class="contents signature-content">
      <div class="signature">
        <Signature ref="esign" :width="screenHeight" :height="screenWidth - 100" v-model:bgColor="bgColor" />

        <div class="btn-area">
          <div class="btns" @click="reset">清除重写</div>
          <div class="btns" @click="comfirm">提交签字</div>
        </div>
        <div class="back" @click="showSignature = false">
          <van-icon name="arrow-left" />
          <span> 请在下方区域签名 </span>
        </div>
      </div>
    </div>
  </div>

</template>

<script setup lang="ts">
import { ref, nextTick, defineProps, defineEmits, onMounted } from 'vue';
import { showToast } from 'vant';
import Signature from './Signature.vue';
import { getToday, dataURLtoFile } from '../assets/js/common';
const screenWidth = ref<number>(0);
const screenHeight = ref<number>(0);
const left = ref<string>('');
const top = ref<string>('');
const clickStatus = JSON.parse(localStorage.getItem('clickStatus'));

const currentDay = ref(clickStatus.currentDay?.split("-") || "");
console.log(currentDay)
onMounted(() => {
  screenWidth.value = document.body.clientWidth;
  screenHeight.value = document.body.clientHeight;
  //left.value = (screenWidth.value - screenHeight.value) / 2 - 40 + 'px';
  //top.value = (screenHeight.value - screenWidth.value) / 2 + 30 + 'px';
});

// 179.5   -241
const bgColor = ref<string>('#f5f7fb');
const esign = ref<any>(null);
const showSignature = ref<boolean>(false);
const signature = ref<string>('');
const props = defineProps(['contentList', 'agreementTitle', 'indexList']);
const emits = defineEmits(['doSubmit']);
const startSignature = async () => {
  await nextTick();
  showSignature.value = true;
};
const reset = () => {
  esign?.value?.reset();
};
const comfirm = async () => {
  try {
    const res = await esign?.value?.generate();
    signature.value = res;
    showSignature.value = false;
  } catch (e) {
    showToast('请先签名');
  }
};

const dosub = async () => {
  const dataUrls = signature.value;
  // const node = document.querySelector('#app') as HTMLDivElement;
  // NProgress.start();
  // const dataUrls = await toPng(node);
  const file = await dataURLtoFile(dataUrls, props.agreementTitle + '.png');
  // NProgress.done();
  emits('doSubmit', file);
};
</script>

<style lang="less" scoped>
#agreement {
  width: 100vw;
  height: calc(100vh - 50px);
  //overflow: auto;
  padding: 17px;
  box-sizing: border-box;
  background: #fff;
  color: #333;
  padding-top: 60px;
}

.title {
  width: 100%;
  height: 40px;
  opacity: 1;
  font-size: 18px;
  font-weight: normal;
  line-height: 25px;
  text-align: center;
  letter-spacing: 0em;
  font-variation-settings: 'opsz' auto;
  color: #212121;
}

.content {
  line-height: 30px;
  font-size: 16px;

  .items {
    margin-top: 5px;
  }
}

.mt-30 {
  font-size: 18px;
  font-weight: 600;
}

.contents {
  &:after {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    width: 200%;
    height: 200%;
    background: rgba(255, 255, 255, 1);
    z-index: 0;
  }
}

.signature {
  position: fixed;
  top: v-bind(top);
  left: v-bind(left);
  width: 100vh;
  height: 100vw;
  transform: rotate(90deg);
  background: #fff;
  transform-origin: center;
  display: flex;
  flex-direction: column;
  flex-flow: column-reverse;
  padding: 20px;
  z-index: 999;


}


.btn-area {
  position: absolute;
  top: 20px;
  right: 75px;
  display: flex;

  .btns {
    line-height: 44px;
    text-align: center;
    color: #fff;
    width: 111px;
    height: 44px;
    background: #209ee4;
    border-radius: 5px;

    &:nth-child(1) {
      margin-right: 20px;
      color: #f56c56;
      background: #f5f7fb;
      border: 1px solid #b3c0c8;
    }
  }
}

.back {
  position: absolute;
  top: 30px;
  left: 30px;
  display: flex;
  color: #333;
  font-size: 16px;
  display: flex;
  align-items: center;

  span {
    margin-left: 10px;
  }
}

.sign {
  display: flex;
  height: 70px;
  align-items: center;

  .start-sign {
    // color: #409eff;
    width: 173px;
    height: 30px;
    background: #f4f6f8;
    border-radius: 5px;
  }

  .sign-img {
    margin-left: 20px;
    width: 130px;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  }
}

.party-a {
  // margin: 24px 0;
  margin-top: 24px;
  line-height: 26px;
  position: relative;

  img {
    width: 70px;
    position: absolute;
    left: 30%;
    top: -10%;
  }
}

.date {
  span {
    &:nth-child(2n) {
      text-decoration: underline;
      margin: 0 5px;
    }
  }
}

.sub-btn {
  width: 95%;
  height: 44px;
  background: #209ee4;
  border-radius: 5px;
  line-height: 44px;
  text-align: center;
  color: #fff;
  font-size: 14px;
  margin: 0 auto;
  margin-bottom: 10px;
  margin-top: 20px;

  span {
    font-size: 14px !important;
  }
}

.signature-content {
  position: fixed;
  top: 0;
  left: 0;
  background-color: #fff;
  height: 100vh;
  width: 100vw;
  z-index: 99999;
  display: flex;
  justify-content: center;
  align-items: center;

  .signature {
    flex: 1
  }
}
</style>
